package com.agriculture.repository;

import com.agriculture.entity.GeoDevicePath;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface GeoDevicePathRepository extends JpaRepository<GeoDevicePath, Integer> {
    
    List<GeoDevicePath> findByDeviceId(Integer deviceId);
    
    @Query("SELECT p FROM GeoDevicePath p WHERE p.deviceId = :deviceId AND p.startTime >= :startTime AND p.endTime <= :endTime")
    List<GeoDevicePath> findByDeviceIdAndTimeRange(@Param("deviceId") Integer deviceId, 
                                                   @Param("startTime") LocalDateTime startTime, 
                                                   @Param("endTime") LocalDateTime endTime);
    
    @Query(value = "SELECT * FROM geo_device_path WHERE ST_Intersects(path_line, ST_GeomFromText(:polygonWkt, 4326))", 
           nativeQuery = true)
    List<GeoDevicePath> findPathsIntersectingPolygon(@Param("polygonWkt") String polygonWkt);
}